<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2018 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2018/11/26
 * Time: 7:00 PM
 */

namespace App\Library\Mall\Item;


use App\Models\ShopCatlogItems;
use App\Repositories\Contracts\ItemRepositoryInterface;
use App\Traits\Common\AuthenticatedUser;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;

trait ItemManagers
{
    use AuthenticatedUser;
    /**
     * @return ItemRepositoryInterface
     */
    protected function itemRepository()
    {
        return app(ItemRepositoryInterface::class)->withoutGlobalScopes();
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request)
    {
        $itemid = $request->input('itemid', 0);
        if ($itemid) {
            $item = $this->itemRepository()->where('uid', $request->user()->uid)->findOrFail($itemid);
            $item->update($request->input('item', []));
        } else {
            $item = $this->itemRepository()->create($request->input('item', []));
        }
        $this->itemRepository()->updateContent($item, $request->input('content', []));
        $this->itemRepository()->updateImages($item, $request->input('images', []));

        return $this->sendItemSavedResponse($request, $item);
    }

    /**
     * @param Request $request
     * @param $item
     * @return \Illuminate\Http\JsonResponse
     */
    protected function sendItemSavedResponse(Request $request, $item)
    {
        return ajaxReturn(['item' => $item]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request)
    {
        $this->itemRepository()->whereKey($request->input('items',[]))->where('uid',Auth::id())->delete();
        return $this->sendDeletedItemsResponse($request);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    protected function sendDeletedItemsResponse(Request $request)
    {
        return ajaxReturn();
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function toggleSale(Request $request)
    {
        $itemid = $request->input('itemid', 0);
        $item = $this->itemRepository();
        return ajaxReturn(['item' => $item]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function batchUpdate(Request $request, ItemManagerServiceInterface $managerService)
    {
        $items = $request->input('items', []);
        $data = Arr::only($request->all(), ['on_sale', 'price', 'stock']);
        $managerService->batchUpdate($items, $data);

        return $this->sendUpdatedItemsResponse($request);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    protected function sendUpdatedItemsResponse(Request $request)
    {
        return ajaxReturn();
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateCatlog(Request $request)
    {

        $items = $request->input('items', []);
        $catlogs = $request->input('catlogs', []);
        if ($items && $catlogs) {
            foreach ($catlogs as $catid) {
                foreach ($items as $itemid) {
                    $data = [
                        'itemid' => $itemid,
                        'catid' => $catid
                    ];
                    ShopCatlogItems::where($data)->firstOrCreate($data);
                }
            }
        }
        return ajaxReturn();
    }

    /**
     * @param Request $request
     * @param ItemManagerServiceInterface $managerService
     * @return \Illuminate\Http\JsonResponse
     */
    public function get(Request $request, ItemManagerServiceInterface $managerService)
    {
        $itemid = $request->input('itemid', 0);
        return ajaxReturn(['item' => $managerService->getItem($itemid)]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function batchget(Request $request, ItemManagerServiceInterface $managerService)
    {
        $offset = $request->input('offset', 0);
        $count = $request->input('count', 10);
        return ajaxReturn(['items' => $managerService->fetchItems($request, $offset, $count)]);
    }
}
